//
//  YYComposeViewController.swift
//  YYWeibo
//
//  Created by strongwallyi on 16/1/11.
//  Copyright © 2016年 strongwallyi. All rights reserved.
//

import UIKit

class YYComposeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }

    
    @objc private func back(){
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    //MARK- 懒加载
    private lazy var titleView: UILabel = {
        let titleView: UILabel = UILabel()
        
        titleView.numberOfLines = 0
        titleView.textAlignment = .Center
        
        return titleView
    }()
    
    private lazy var rightButton: UIButton = {
        let button = UIButton()
        
        // 设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Disabled)
        button.setBackgroundImage(UIImage(named: "common_button_orange"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_orange_highlighted"), forState: UIControlState.Highlighted)
        
        // 设置不同状态的文字颜色
        
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Disabled)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        
        // 设置文字以及字体大小
        button.setTitle("发送", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        button.frame.size = CGSize(width: 40, height: 30)
        
        return button
    }()
    
    private lazy var textView: YYTextView = {
        
        let textView = YYTextView()
        textView.delegate = self
        textView.placeholder = "请输入想要发表的文字"
        textView.font = UIFont.systemFontOfSize(16)
    
        return textView
    }()
}

//Mark-textView的代理方法
extension YYComposeViewController: UITextViewDelegate {
    //当文字发生改变的时候就会调用这个方法
    func textViewDidChange(textView: UITextView) {
        //判断是否有文字
        navigationItem.rightBarButtonItem?.enabled = textView.hasText()
    }
}

//Mark-加载视图

extension YYComposeViewController{
    
    private func setupUI(){
        view.backgroundColor = UIColor.whiteColor()
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", target: self, action: "back")
        
        //右边发送微博的按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightButton)
        navigationItem.rightBarButtonItem?.enabled = false
        
        //判断用户名是否是存在
        if let name = YYUserAccountViewModel.sharedViewModel.userAccount?.screen_name {
            //要显示的内容
            let titleString = "发微博\n\(name)"
            //通过要显示的内容初始化成可变的富文本
            let attr = NSMutableAttributedString(string: titleString)
            //求出 name 在字符串的位置
            let range = (titleString as NSString).rangeOfString(name)
            //添加属性
            attr.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(14), range: range)
            attr.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGrayColor(), range: range)
            
            titleView.attributedText = attr
            //设置大小
            titleView.sizeToFit()
            navigationItem.titleView = titleView
        }else {
            navigationItem.title = "发微博"
        }
        
        view.addSubview(textView)
        
        //添加约束
        textView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
        
    }
    
}


